package com.ruoyi.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.constant.ScoreConstant;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.annotation.RequiresLogin;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.domain.Activity;
import com.ruoyi.system.domain.vo.IndexSchoolRole;
import com.ruoyi.system.service.IActivityService;
import com.ruoyi.system.service.ISchoolRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.Score;
import com.ruoyi.system.service.IScoreService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 评测表增删改查Controller
 * 
 * @author hyb
 * @date 2023-03-10
 */
@RestController
@RequestMapping("/score")
public class ScoreController extends BaseController
{
    @Autowired
    private IScoreService scoreService;

    @Autowired
    private ISchoolRoleService schoolRoleService;

    @Autowired
    private IActivityService activityService;

    /*
    * 获取用户评分
    * */
    @GetMapping("/getUserScore")
    public AjaxResult getUserScore(@RequestParam("activityId")String activityId){

        Score score = new Score();
        score.setActivityId(Long.parseLong(activityId));
        score.setType(ScoreConstant.USER_SCORE);
        List<Score> scores = scoreService.selectScoreList(score);
        return AjaxResult.success().put("score",scores.size()==0?0:scores.get(0));
    }

    /*
    * 前台新增一条评测
    * TODO: 根据用户评测和活动评测做jsr303分组校验
    * */
    @PostMapping("/activity/insert")
    public AjaxResult frontInsert(@RequestBody Score score){

        //先查这个活动有没有评分过
        Score p = new Score();
        p.setActivityId(score.getActivityId());
        p.setType(ScoreConstant.ACTIVITY_SCORE);
        List<Score> scores = scoreService.selectScoreList(p);
        if (scores.size()!=0){
            return AjaxResult.error("该活动已获得您的评分,不能再次获取");
        }

        //设置用户信息
        Long userId = SecurityUtils.getUserId();

        IndexSchoolRole indexSchoolRole = schoolRoleService.selectSchoolRoleAndSysUserById(userId);

        score.setUserId(userId);
        score.setUserNumber(indexSchoolRole.getNumber());
        score.setRealName(indexSchoolRole.getName());
        //设置活动信息
        Activity activity = activityService.selectActivityById(score.getActivityId());
        score.setActivityName(activity.getName());

        //设置时间
        String s = DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss");
        score.setPf1(s);
        score.setPf2(s);

        scoreService.insertScore(score);

        return AjaxResult.success();

    }

    @PostMapping("/user/insert")
    public AjaxResult userInsert(@RequestBody Score score){


        //先查这个对象有没有已经评分过
        Score p = new Score();
        p.setActivityId(score.getActivityId());
        p.setRateObjId(score.getRateObjId());
        p.setType(ScoreConstant.USER_SCORE);
        List<Score> scores = scoreService.selectScoreList(p);
        if (scores.size()!=0){
            return AjaxResult.error("您已给用户评分,不能再次评分");
        }

        //其他选项可以为空,但是评分必须不能为空
        //设置用户信息
        Long userId = SecurityUtils.getUserId();

        IndexSchoolRole indexSchoolRole = schoolRoleService.selectSchoolRoleAndSysUserById(userId);

        score.setUserId(userId);
        score.setUserNumber(indexSchoolRole.getNumber());
        score.setRealName(indexSchoolRole.getName());
        //设置活动信息
        Activity activity = activityService.selectActivityById(score.getActivityId());
        score.setActivityName(activity.getName());
        //设置被评测人信息
        Long rateObjId = score.getRateObjId();
        IndexSchoolRole obj = schoolRoleService.selectSchoolRoleAndSysUserById(rateObjId);
        score.setRateObjRealName(obj.getName());
        score.setRateObjNumber(obj.getNumber());
        //设置时间
        String s = DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss");
        score.setPf1(s);
        score.setPf2(s);

        //
        scoreService.insertScore(score);



        return AjaxResult.success();
    }


    /**
     * 查询评测表增删改查列表
     */
    @RequiresPermissions("score:score:list")
    @GetMapping("/list")
    public TableDataInfo list(Score score)
    {
        startPage();
        List<Score> list = scoreService.selectScoreList(score);
        return getDataTable(list);
    }

    /**
     * 导出评测表增删改查列表
     */
    @RequiresPermissions("score:score:export")
    @Log(title = "评测表增删改查", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Score score)
    {
        List<Score> list = scoreService.selectScoreList(score);
        ExcelUtil<Score> util = new ExcelUtil<Score>(Score.class);
        util.exportExcel(response, list, "评测表增删改查数据");
    }

    /**
     * 获取评测表增删改查详细信息
     */
    @RequiresPermissions("score:score:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(scoreService.selectScoreById(id));
    }

    /**
     * 新增评测表增删改查
     */
    @RequiresPermissions("score:score:add")
    @Log(title = "评测表增删改查", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Score score)
    {
        return toAjax(scoreService.insertScore(score));
    }

    /**
     * 修改评测表增删改查
     */
    @RequiresPermissions("score:score:edit")
    @Log(title = "评测表增删改查", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Score score)
    {
        return toAjax(scoreService.updateScore(score));
    }

    /**
     * 删除评测表增删改查
     */
    @RequiresPermissions("score:score:remove")
    @Log(title = "评测表增删改查", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(scoreService.deleteScoreByIds(ids));
    }
}
